home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / 7Edit 3.1 / Sources / SVAppleEvents.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  9.3 KB  |  291 lines  |  [TEXT/CWIE]

  1. // SVAppleEvents.c
  2. //
  3. // 7Edit 3.1d1. Original version by Jon Lansdell and Nigel Humphreys.
  4. // 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1995, all rights reserved.
  6.  
  7. #include "SVAppleEvents.h"
  8.  
  9. #include <Resources.h>
  10.  
  11. #include "SVEditGlobals.h"
  12. #include "SVEditUtils.h"
  13. #include "SVEditAEUtils.h"
  14. #include "SVEditWindow.h"
  15. #include "SVEditFile.h"
  16.  
  17. #include "SVAERecording.h"
  18. #include "SVEditGXPrinting.h"
  19.  
  20. #include "SVAECompare.h"
  21. #include "SVAECountElements.h"
  22. #include "SVAEAccessors.h"
  23. #include "SVAECoercions.h"
  24. #include "SVAEObjectsExist.h"
  25. #include "SVAECreate.h"
  26. #include "SVAEClone.h"
  27. #include "SVAEMove.h"
  28. #include "SVAEDelete.h"
  29. #include "SVAECopy.h"
  30. #include "SVAECut.h"
  31. #include "SVAEPaste.h"
  32. #include "SVAESelect.h"
  33. #include "SVAEClose.h"
  34. #include "SVAESave.h"
  35. #include "SVAERevert.h"
  36. #include "SVAEGetData.h"
  37. #include "SVAESetData.h"
  38. #include "SVAEGetDataSize.h"
  39.  
  40. short        gRefNum;
  41.  
  42. #pragma segment Main
  43.  
  44. /*-----------------------------------------------------------------------*/
  45. /**----------                         APPLE EVENT HANDLING                     ---------------**/
  46. /*-----------------------------------------------------------------------*/
  47.  
  48.  
  49. // -----------------------------------------------------------------------
  50. //        Name:             DoAppleEvent
  51. //        Purpose:        Process and despatch the AppleEvent
  52. // -----------------------------------------------------------------------
  53.  
  54. void DoAppleEvent(EventRecord theEvent)
  55. {
  56.     OSErr err;
  57.  
  58.   // should check for your own event message types here - if you have any
  59.     
  60.     err = AEProcessAppleEvent(&theEvent);
  61. }
  62.  
  63. // -----------------------------------------------------------------------
  64. //        Name:             DoOpenApp
  65. //        Purpose:        Called on startup, creates a new document.
  66. // -----------------------------------------------------------------------
  67.  
  68. pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
  69.   {
  70. #pragma unused (reply,refcon, message)
  71.     
  72.       DPtr ourDoc;
  73.     
  74.         /*just create a new document*/
  75.         ourDoc = NewDocument(false, (WindowPtr)-1L);
  76.         
  77.         if (ourDoc)
  78.             {
  79.                 ShowWindow(ourDoc->theWindow);
  80.                 return(noErr);
  81.             }
  82.         else
  83.             return(-108);
  84.     }
  85.  
  86. // -----------------------------------------------------------------------
  87. //        Name:             DoOpenDocument
  88. //        Purpose:        Open all the documents passed in the Open AppleEvent.
  89. // -----------------------------------------------------------------------
  90.  
  91. pascal OSErr DoOpenDocument(const AppleEvent *message, const AppleEvent *reply, long refcon)            
  92.   {
  93. #pragma unused (reply, refcon)
  94.  
  95.         long        index;
  96.         long        itemsInList;
  97.         AEKeyword   keywd;
  98.         OSErr       err;
  99.         OSErr       ignoreErr;
  100.         AEDescList  docList;
  101.         long        actSize;
  102.         DescType    typeCode;
  103.         FSSpec      theFSSpec;
  104.         
  105.         /*open the specified documents*/
  106.         
  107.         docList.dataHandle = nil;
  108.         
  109.         err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  110.         
  111.         if (err==noErr)
  112.             err = AECountItems( &docList, &itemsInList) ;
  113.         else
  114.           itemsInList = 0;
  115.             
  116.         for (index = 1; index <= itemsInList; index++)
  117.             if (err==noErr)
  118.                 {
  119.                     err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  120.                                                          (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
  121.                     if (err==noErr)
  122.                       err = OpenOld(theFSSpec);
  123.                 }
  124.     
  125.       if (docList.dataHandle)
  126.             ignoreErr = AEDisposeDesc(&docList);
  127.             
  128.         return(err);
  129.     }
  130.  
  131. // -----------------------------------------------------------------------
  132. //        Name:             MyQuit
  133. //        Purpose:        Quit event received- exit the program.
  134. // -----------------------------------------------------------------------
  135.  
  136. pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)            
  137.     {
  138. #pragma unused (reply,refcon)
  139.     
  140.         DescType saveOpt;
  141.         OSErr    tempErr;
  142.         OSErr    myErr;
  143.         DescType returnedType;
  144.         long     actSize;
  145.         
  146.         saveOpt = kAEAsk; /* the default */
  147.         tempErr = AEGetParamPtr(message,
  148.                                                         keyAESaveOptions,
  149.                                                         typeEnumerated,
  150.                                                         &returnedType,
  151.                                                         (Ptr)&saveOpt,
  152.                                                         sizeof(saveOpt),
  153.                                                         &actSize);
  154.         
  155.         if (saveOpt != kAENo)
  156.             myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  157.  
  158.       if (myErr == noErr)
  159.             DoQuit(saveOpt);
  160.         
  161.         return(myErr);
  162.     }
  163.  
  164.  
  165. // -----------------------------------------------------------------------
  166. //    Name:             DoPrintDocuments
  167. //    Purpose:        Print a list of documents (or windows).
  168. // -----------------------------------------------------------------------
  169.  
  170. pascal OSErr DoPrintDocuments(const AppleEvent *message, AppleEvent *reply, long refcon)
  171. {
  172. #pragma unused (reply, refcon)
  173.     long          index;
  174.     long          itemsInList;
  175.     AEKeyword     keywd;
  176.     OSErr         err;
  177.     AEDescList    docList;
  178.     Size          actSize;
  179.     DescType      typeCode;
  180.     FSSpec        theFSSpec;
  181.     WindowToken   theWindowToken;
  182.     OSErr         forgetErr;
  183.     Boolean       talkToUser;
  184.         
  185.     err = AEGetParamDesc(message,
  186.                                              keyDirectObject,
  187.                                              typeAEList,
  188.                                              &docList);
  189.                                              
  190.     err = AECountItems(&docList, &itemsInList);
  191.         
  192.     for (index = 1; index<=itemsInList; index++)
  193.         if (err == noErr) 
  194.             {
  195.                 forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
  196.                                                                  &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  197.                                                                     
  198.                 talkToUser = false;        // (AEInteractWithUser(kAEDefaultTimeout, nil, nil) == noErr);
  199.     
  200.                 if (forgetErr == noErr) 
  201.                     {
  202.                         if (err == noErr) 
  203.                             err = IssueAEOpenDoc(theFSSpec);
  204.                             
  205.                         if (err == noErr) 
  206.                             IssuePrintWindow(FrontWindow(), talkToUser);
  207.                             
  208.                         if (err == noErr) 
  209.                             IssueCloseCommand(FrontWindow());
  210.                     }
  211.                 else
  212.                     { /* wasn't a file - was it a window ? */
  213.                         err = AEGetNthPtr(&docList,
  214.                                           index,
  215.                                           typeMyWndw,
  216.                                           &keywd,
  217.                                                             &typeCode,
  218.                                                             (Ptr)&theWindowToken,
  219.                                                             sizeof(WindowToken),
  220.                                                             &actSize);
  221.                                                                 
  222.                                                                                             
  223.                         if (err == noErr)
  224.                             if (gGXIsPresent)
  225.                                 err = GXPrintDocument(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
  226.                             else
  227.                                 PrintWindow(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
  228.                     }
  229.             }
  230.     
  231.     if (docList.dataHandle)
  232.         forgetErr = AEDisposeDesc(&docList);
  233.         
  234.     return(err);
  235. } /* DoPrintDocuments */
  236.  
  237.  
  238. // -----------------------------------------------------------------------
  239. //    Name:             InitAppleEvents
  240. //    Purpose:        Initialise the AppleEvent despatch table
  241. // -----------------------------------------------------------------------
  242.  
  243. void    InitAppleEvents(void)
  244. {
  245.     OSErr aevtErr;
  246.     
  247.     gRefNum = CurResFile();        // Needed for getting application property
  248.     
  249.         // set up the dispatch table for the four standard AppleEvents
  250.     
  251.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
  252.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
  253.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
  254.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
  255.     
  256.         // set up the dispatch table for the core AppleEvents
  257.  
  258.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESetData,                 NewAEEventHandlerProc(DoSetData),   noRefCon, false);
  259.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetData,         NewAEEventHandlerProc(DoGetData),   noRefCon, false);
  260.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetDataSize,     NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
  261.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,   NewAEEventHandlerProc(DoCountElements),   noRefCon, false);
  262.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  NewAEEventHandlerProc(DoObjectsExist),   noRefCon, false);
  263.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,   NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
  264.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClone,           NewAEEventHandlerProc(DoClone),   noRefCon, false);
  265.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDelete,          NewAEEventHandlerProc(DoDelete),noRefCon, false);
  266.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEMove,                       NewAEEventHandlerProc(DoMove),   noRefCon, false);
  267.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClose,           NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
  268.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESave,            NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
  269.  
  270.         // set up the dispatch table for the miscellaneous AppleEvents
  271.     
  272.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCut),   noRefCon, false);
  273.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopy),  noRefCon, false);
  274.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPaste), noRefCon, false);
  275.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevert),noRefCon, false);
  276.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAESelect, NewAEEventHandlerProc(DoSelect),noRefCon, false);
  277.  
  278.         // Install recording handlers
  279.     aevtErr = InstallRecordingHandlers();
  280.     
  281.         // Install callbacks for count and compare procedures
  282.     aevtErr = InstallObjectCallbacks();
  283.     
  284.         // Now install our object accessors
  285.     aevtErr = InstallAccessors();
  286.  
  287.         // Now the coercion handlers
  288.     aevtErr = InstallCoercions();
  289.  
  290. } // InitAppleEvents
  291.